package org.example.JUC;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest {

    private final static Semaphore semaphore = new Semaphore(4);

    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {
            new Worker().start();
        }
    }

    static class Worker extends Thread {

        @Override
        public void run() {
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName() + "-ing");
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                semaphore.release();
            }
        }
    }
}
